package com.active.web;

import com.active.service.AjaxPageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Created by Administrator on 2019/6/18 0018.
 */
@RestController
@RequestMapping("sunFlow")
@Slf4j
public class SUNFlowController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    @RequestMapping("mail")
    public AjaxPageResult mail(){

        for(int i=1;i<101;i++){
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            try {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
                //邮件设置
                helper.setFrom(String.valueOf(new InternetAddress("你的意中人<"+from+">")));
                helper.setTo("550296843@qq.com");

                helper.setSubject("第"+i+"封久违的情书");
                helper.setText("【意中人】 唱山歌: <font color=red>"+"送你来到我身边"+"</font> (动态情书),请在3分钟内完成验证",true);

                javaMailSender.send(mimeMessage);
                log.info("邮件已发送"+i);
            }catch (Exception e){
                return AjaxPageResult.fail("邮箱不存在");
            }
        }
        return AjaxPageResult.success("邮件已发送完成");
    }
}
